<?php
namespace app\app\controller\v1;
use think\Db;
use app\app\util\OnlineUtils;
use app\app\util\ShareUtils;

class ActController extends BaseController{


    /**
     * 创建活动
     * @return array
     */
    public function active()
    {
        $act['user_id'] = OnlineUtils::getUserIdBySid();
        $act['created_at'] = time();
        $new_act = Db::name('act')->where('user_id','eq',$act['user_id'])->order('id desc')->find();
//        $new_act = Db::name('act')->where('user_id','eq',$act['user_id'])->find();
//
//        if ( !$act_list ){
//            $id = DB::name('act')->insertGetId($act);  //活动id
//            return ft(200,'success',['user_id'=>$act['user_id'],'act_id'=>$id]);
//            //如果当前时间小于结束时间
//        }elseif ( isset($act_list) && $act['created_at']<$act_list['end_time']){
//            return ft(402,'已存在活动，请等待活动结束再发起活动',$act_list);
//        }

        if( !$new_act ){
            $id = DB::name('act')->insertGetId($act);  //活动id
            return ft(200,'success',['user_id'=>$act['user_id'],'act_id'=>$id]);
        }elseif ( isset($new_act)){
           if( $new_act['start_time'] == 0 ){
                return ft(402,'你还有活动未开始请先开始活动',$new_act);
            }elseif ($new_act['start_time'] < time()  && time() < $new_act['end_time']){
                return ft(402,'活动进行中，请等待活动结束再发起活动',$new_act);
            }elseif( time() > $new_act['end_time'] ){
                $id = DB::name('act')->insertGetId($act);  //活动id
                return ft(200,'success',['user_id'=>$act['user_id'],'act_id'=>$id]);
            }
        }
        return ft(200,'success',$new_act);
    }

    /**
     * @return array
     * 开始活动
     */
    public function startAct()
    {
        $user_id = OnlineUtils::getUserIdBySid();   //用户id
        $act_id = input('get.act_id');              //活动id
        $act = Db::name('act')->where('status','eq',1)->find($act_id);
        //开启活动
        if ( $user_id !== $act['user_id'] ){
            return ft(402,'您不是活动发起人，无法开始活动');
        }else{
            $start_time  = time();                   //开始时间
            $end_time = $start_time + 259200;        //结束时间
            //修改开始时间、结束时间、活动状态（1未开始 2进行中 3已结束）
            Db::query('update ft_act set start_time = '.$start_time.',end_time = '.$end_time.',status = 2 where id ='.$act_id);
            $act_info = Db::name('act')->alias('a')->join('user u','a.user_id = u.id','left')
                ->field('a.*,u.nickname,u.avatar_url')
                ->where('a.id','eq',$act_id)
                ->find();
            return ft(200,'success',$act_info);
        }

    }


    /**
     * @return array
     * 活动点击
     */
    public function clickAct()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $act_id = input('get.act_id');
        $click_time = time();
        //判断是否已经助力
        $click = Db::name('act_click')->where('user_id','eq',$user_id)->where('act_id','eq',$act_id)->find();
        if ( $click ){
            return ft(402,'您已经为该活动助力，请勿重复操作');
        }
        //判断自己不能给自己助力
        $act = Db::name('act')->where('id','eq',$act_id)->find();
        if ( $user_id == $act['user_id']){
            return ft(402,'无法为自己助力');
        }
        //查询活动信息
        $act = Db::name('act')->where('id','eq',$act_id)->find();
        if ( $act['status'] !== 2){
            return ft(402,'活动未开始或已结束，无法助力');
        }
        if ( $act['start_time']<$click_time &&  $click_time<$act['end_time'] ){
            $data = array('act_id'=>$act_id,'user_id'=>$user_id,'click_time'=>$click_time);
            $id = DB::name('act_click')->insertGetId($data);  //活动id
            return ft(200,'success',$id);
        }
    }


    /**
     * @return array
     * 点击列表
     */
    public function clickList()
    {
        $act_id = input('get.act_id');
        $time = time();
        //活动详情
        $act = Db::name('act')->alias('a')->join('user u','u.id = a.user_id','left')
            ->field('a.*,u.nickname,u.avatar_url')
            ->where('a.id','eq',$act_id)->find();
        //助力列表
        $click_list = Db::name('act_click')->alias('a')->join('user u','u.id = a.user_id','left')
            ->field('a.*,u.nickname,u.avatar_url')
            ->where('a.act_id','eq',$act_id)->select();
        //助力人数
        $click_num = Db::name('act_click')->where('act_id','eq',$act_id)->count();
        //当前时间大于活动结束时间状态改为已结束
        if( $time > $act['end_time'] ){
            Db::query('update ft_act set status = 3 where id ='.$act_id);
            $act = Db::name('act')->alias('a')->join('user u','u.id = a.user_id','left')
            ->field('a.*,u.nickname,u.avatar_url')
            ->where('a.id','eq',$act_id)->find();
            if ( $act['status'] == 1 ) {
                $act['status'] = '活动未开始';
            }elseif ($act['status'] == 2 ){
                $act['status'] = '活动进行中';
            }elseif ($act['status'] == 3 ){
                $act['status'] = '活动已结束';
            }
        }
        return ft(200,'success',['click_list'=>$click_list,'click_num'=>$click_num,'now_time'=>$time,'act'=>$act]);
    }

}